<template>
  <div class="app-container">
<!--    {{id}}-->
    <el-form ref="form" :model="user" label-width="80px">
      <el-form-item label="用户姓名">
        <el-input v-model="user.username"  style="width: 217px;" :disabled="true"></el-input>
      </el-form-item>

      <el-form-item label="用户余额">
        <el-input v-model="user.money"  style="width: 217px;" ></el-input>
      </el-form-item>
<!--      <select v-model="state" placeholder="用户性别">-->
<!--        <option value="1">男</option>-->
<!--        <option value="2">女</option>-->
<!--      </select>-->



      <el-form-item>
<!--        给修改前加上一个判断条件 是否需要修改-->
        <el-button type="primary" @click="onSubmit">修改</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<style>
  /*#app{*/
  /*  text-align: left;*/
  /*}*/
  .el-form{
    float: left;
  }
</style>

<script>
import axios from "axios";

export default {
  name: "roleEditView",
  data(){       //data:{}
    return {
      user:{
        id:'',
        username:'',
        money:"",
      },


    }
  },
  //获取数据
  created(){
    //通过this.$route.query.id获取当前路由参数中的id值

    this.user.id=parseInt(this.$route.query.id)
    axios({
      url: '/api/user/queryById/'+this.user.id,
      method: 'post',
      data: this.user,
      headers:{"Authorization":"Bearer "+this.$store.state.user.token}
    }).then(res => {
      console.log(res.data.data)
      this.user.username=res.data.data[0].username
      this.user.money=res.data.data[0].money
      this.user.id=res.data.data[0].id
    })


  },

  methods:{
    //修改数据
    onSubmit(){
      //创建表单对象
      // var formData = new FormData();

      //创建表格对象
      var formData = new window.FormData()
      formData.append("id",this.user.id)
      formData.append("money",this.user.money)
      //将fileList中的文件添加到formData中 循环添加文件对象中的raw 后台接收的二进制
      // for (const key in this.fileList) {
      //   formData.append("files",this.fileList[key].raw)
      // }

        // axios({
        //   url:"/api/user/edit",
        //   method:"post",
        //   data:formData,
        //   //添加请求头
        //   headers:{"Content-Type":"multipart/form-data"}
        // }).then(res=>{
        //   this.$message({
        //     message: '修改成功',
        //     type: 'success'
        //   });
        //     //返回医生信息表
        //     this.$router.push("/role")
        // })

        // console.log(farmData)
      this.$confirm('是否确定修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        axios({
          url:"/api/user/editMoney",
          method:"post",
          data:formData,
          //添加请求头
          // headers:{"Content-Type":"multipart/form-data"}
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res=>{
          if(res.data.code==1){
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            //返回余额
            this.$router.push("/user/userMoneyView")
          }else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });


    },
    cancel(){
      this.$router.push("/user/userMoneyView")
    },



  }

}
</script>

<style scoped>

</style>
